package com.cly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cly.domain.UserCollection;
import com.cly.es.ProdEs;
import com.cly.feign.CollectSearchFeign;
import com.cly.mapper.UserCollectionMapper;
import com.cly.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Autowired
    private CollectSearchFeign collectSearchFeign;

    /**
     * 判断用户是否收藏
     *
     * @param prodId
     * @param userId
     * @return
     */
    @Override
    public Boolean isCollection(Long prodId, String userId) {
        Integer count = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, userId)
        );
        return count > 0;
    }

    /**
     * 用户新增或者取消收藏
     *
     * @param prodId
     * @param userId
     */
    @Override
    public void addOrCancelCollection(Long prodId, String userId) {
        // 查询数据库 有没有收藏
        UserCollection userCollection = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, userId)
        );
        if (ObjectUtils.isEmpty(userCollection)) {
            UserCollection userCollection1 = new UserCollection();
            // 新增一条
            userCollection1.setProdId(prodId);
            userCollection1.setUserId(userId);
            userCollection1.setCreateTime(new Date());
            userCollectionMapper.insert(userCollection1);
        } else {
            userCollectionMapper.deleteById(userCollection.getId());
        }
    }

    /**
     * 获取用户收藏的商品数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer countCollection(String userId) {
        Integer count = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
        );
        return count;
    }

    /**
     * 分页查询用户收藏商品信息
     * 1.常规的分页查询收藏
     * 2.拿到商品ids
     * 3.远程调用搜索服务 根据ids 查询商品对象
     * 4.组装返回
     *
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Page<ProdEs> getUserCollectPage(Page<UserCollection> page, String userId) {
        Page<ProdEs> prodEsPage = new Page<>(page.getCurrent(), page.getSize());
        Page<UserCollection> userCollectionPage = userCollectionMapper.selectPage(page, new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
        );
        List<UserCollection> userCollections = userCollectionPage.getRecords();
        if (CollectionUtils.isEmpty(userCollections)) {
            return prodEsPage;
        }
        // 拿到商品ids
        List<Long> prodIds = userCollections.stream()
                .map(UserCollection::getProdId)
                .collect(Collectors.toList());
        // 远程调用搜索服务 拿到list<ProdEs>
        List<ProdEs> collectProdEs = collectSearchFeign.getProdEsByIds(prodIds);
        prodEsPage.setRecords(collectProdEs);
        prodEsPage.setTotal(userCollectionPage.getTotal());
        return prodEsPage;
    }
}
